package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.EquipmentAudit;
import com.mxpio.erp.common.equipment.service.EquipmentAuditService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.Order;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentAuditController}", description = "稽核管理")
@RestController
@RequestMapping("/erp/equipment/audit/")
public class EquipmentAuditController {

    @Autowired
    private EquipmentAuditService equipmentAuditService;


    @GetMapping("list")
    @Operation(summary = "稽核管理列表", description = "获取稽核管理列表（无分页）", method = "GET")
    public Result<List<EquipmentAudit>> list(@Parameter(description = "查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentAudit> list = equipmentAuditService.list(EquipmentAudit.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("apply/page")
    @Operation(summary = "稽核管理列表-稽核申请", description = "获取稽核管理列表-稽核申请（分页）", method = "GET")
    public Result<Page<EquipmentAudit>> applyPage(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        String username = SecurityUtils.getLoginUsername();
        criteria = criteria.or()
                .addCriterion("auditorId", Operator.EQ, username)
                .addCriterion("createBy", Operator.EQ, username)
                .addCriterion("rectId", Operator.EQ, username)
                .addCriterion("accpId", Operator.EQ, username)
                .addCriterion("managerId", Operator.EQ, username)
                .end()
                .addOrder(new Order("updateTime", true))
                .addOrder(new Order("createTime", true));
        Pageable page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentAudit> pageResult = equipmentAuditService.listPage(EquipmentAudit.class, page, criteria);
        return Result.OK(pageResult);
    }
    @GetMapping("send/page")
    @Operation(summary = "稽核管理列表-稽核派工", description = "获取稽核管理列表-稽核派工（分页）", method = "GET")
    public Result<Page<EquipmentAudit>> sendPage(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        String username = SecurityUtils.getLoginUsername();
        criteria = criteria
                .addCriterion("managerId", Operator.EQ, username)
                .addCriterion("orderStatus", Operator.EQ, EquipmentEnums.AuditStatus.APPLY.getCode())
                .addOrder(new Order("updateTime", true))
                .addOrder(new Order("createTime", true));
        Pageable page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentAudit> pageResult = equipmentAuditService.listPage(EquipmentAudit.class, page, criteria);
        return Result.OK(pageResult);
    }
    @GetMapping("rect/page")
    @Operation(summary = "稽核管理列表-稽核整改", description = "获取稽核管理列表-稽核整改（分页）", method = "GET")
    public Result<Page<EquipmentAudit>> rectPage(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        String username = SecurityUtils.getLoginUsername();
        criteria = criteria
                .addCriterion("rectId", Operator.LIKE, username)
                .addCriterion("orderStatus", Operator.EQ, EquipmentEnums.AuditStatus.SEND.getCode())
                .addOrder(new Order("updateTime", true))
                .addOrder(new Order("createTime", true));
        Pageable page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentAudit> pageResult = equipmentAuditService.listPage(EquipmentAudit.class, page, criteria);
        return Result.OK(pageResult);
    }
    @GetMapping("check/page")
    @Operation(summary = "稽核管理列表-稽核验收", description = "获取稽核管理列表-稽核验收（分页）", method = "GET")
    public Result<Page<EquipmentAudit>> checkPage(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        String username = SecurityUtils.getLoginUsername();
        criteria = criteria.or()
                .addCriterion("auditorId", Operator.EQ, username)
                .addCriterion("managerId", Operator.EQ, username)
                .end()
                .addCriterion("orderStatus", Operator.EQ, EquipmentEnums.AuditStatus.AUDIT.getCode())
                .addOrder(new Order("updateTime", true))
                .addOrder(new Order("createTime", true));
        Pageable page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentAudit> pageResult = equipmentAuditService.listPage(EquipmentAudit.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("record/page")
    @Operation(summary = "稽核管理列表-稽核履历", description = "获取稽核管理列表-稽核履历（分页）", method = "GET")
    public Result<Page<EquipmentAudit>> recordPage(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        String username = SecurityUtils.getLoginUsername();
        criteria = criteria.or()
                .addCriterion("auditorId", Operator.EQ, username)
                .addCriterion("createBy", Operator.EQ, username)
                .addCriterion("rectId", Operator.EQ, username)
                .addCriterion("accpId", Operator.EQ, username)
                .addCriterion("managerId", Operator.EQ, username)
                .end()
                .addCriterion("orderStatus", Operator.EQ, EquipmentEnums.AuditStatus.CHECKED.getCode())
                .addOrder(new Order("updateTime", true))
                .addOrder(new Order("createTime", true));
        Pageable page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentAudit> pageResult = equipmentAuditService.listPage(EquipmentAudit.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取稽核管理列表", description = "根据ID获取稽核管理列表", method = "GET")
    public Result<EquipmentAudit> getById(@PathVariable(name = "id", required = true) String id) {
        EquipmentAudit equipmentAudit = equipmentAuditService.getById(EquipmentAudit.class, id);
        return Result.OK(equipmentAudit);
    }

    @RequestMapping("add")
    @Operation(summary = "保存稽核管理", description = "保存稽核管理")
    public Result<EquipmentAudit> add(@RequestBody EquipmentAudit equipmentAudit) {
        equipmentAuditService.saveAudit(equipmentAudit);
        return Result.OK(equipmentAudit);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑稽核管理", description = "编辑稽核管理", method = "PUT")
    public Result<EquipmentAudit> edit(@RequestBody EquipmentAudit equipmentAudit) {
        equipmentAuditService.update(equipmentAudit);
        return Result.OK(equipmentAudit);
    }

    @PutMapping("send")
    @Operation(summary = "编辑稽核管理-派工", description = "编辑稽核管理-派工", method = "PUT")
    public Result<?> send(@RequestBody EquipmentAudit equipmentAudit) {
        equipmentAuditService.send(equipmentAudit);
        return Result.OK("派工成功!");
    }

    @PutMapping("reSend")
    @Operation(summary = "编辑稽核管理-改派", description = "编辑稽核管理-改派", method = "PUT")
    public Result<?> reSend(@RequestBody EquipmentAudit equipmentAudit) {
        equipmentAuditService.reSendAuditById(equipmentAudit.getOverReason(), equipmentAudit.getRectId(), equipmentAudit.getId());
        return Result.OK("改派成功!");
    }

    @PutMapping("finish")
    @Operation(summary = "编辑稽核管理-整改完成", description = "编辑稽核管理-整改完成", method = "PUT")
    public Result<?> finish(@RequestBody EquipmentAudit equipmentAudit) {
        equipmentAuditService.finish(equipmentAudit);
        return Result.OK("维修结束!");
    }

    @PutMapping("check")
    @Operation(summary = "编辑稽核管理-验收完成", description = "编辑稽核管理-验收完成", method = "PUT")
    public Result<?> check(@RequestBody EquipmentAudit equipmentAudit, HttpServletRequest request) {
        equipmentAuditService.check(equipmentAudit.getId(), request);
        return Result.OK("验收完成!");
    }

    @PutMapping("reject")
    @Operation(summary = "编辑稽核管理-验收驳回", description = "编辑稽核管理-验收驳回", method = "PUT")
    public Result<?> reject(@RequestBody EquipmentAudit equipmentAudit) {
        equipmentAuditService.reject(equipmentAudit.getId(), equipmentAudit.getOverReason());
        return Result.OK("验收驳回!");
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除稽核管理", description = "删除稽核管理,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentAudit> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for (String key : id) {
            equipmentAuditService.delete(EquipmentAudit.class, key);
        }
        return Result.OK();
    }


}
